package com.fly.controller;

import com.fly.bean.po.ElectricityConsumptionPo;
import com.fly.bean.request.ElectricityConsumptionRequest;
import com.fly.bean.result.AABillResult;
import com.fly.bean.result.CommonResult;
import com.fly.service.ElectricityConsumptionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/ec")
public class ElectricityConsumptionController {
    //属性注入
    @Autowired
    private ElectricityConsumptionService electricityConsumptionService;


    @GetMapping("/testInsert")
    public void testInsert() {
        electricityConsumptionService.testInsert();
    }

    @PostMapping("/queryByMonth")
    public CommonResult queryByMonth(@RequestBody ElectricityConsumptionRequest request) {
        if (ObjectUtils.isEmpty(request)) {
            return CommonResult.fail("关键参数为空！");
        }
        if (StringUtils.isBlank(request.getYearAndMonth())) {
            throw new RuntimeException("关键参数为空！");
        }
        try {
            ElectricityConsumptionPo po = electricityConsumptionService.queryByMonth(request.getYearAndMonth());
            return CommonResult.ok(po);
        } catch (Exception e) {
            log.info("ElectricityConsumptionController.queryByMonth:", e);
            return CommonResult.fail(e.getMessage());
        }
    }

    @PostMapping("/insertNewData")
    public CommonResult insertNewData(@RequestBody ElectricityConsumptionRequest request) {
        if (ObjectUtils.isEmpty(request)) {
            return CommonResult.fail("关键参数为空！");
        }
        if (StringUtils.isBlank(request.getYearAndMonth())) {
            return CommonResult.fail("关键参数为空！");
        }
        try {
            electricityConsumptionService.insertNewData(request);
            return CommonResult.ok();
        } catch (Exception e) {
            log.info("ElectricityConsumptionController.insertNewData:", e);
            return CommonResult.fail(e.getMessage());
        }
    }

    @PostMapping("/AABill")
    public CommonResult AABill(@RequestBody ElectricityConsumptionRequest request) {
        if (ObjectUtils.isEmpty(request)) {
            return CommonResult.fail("关键参数为空！");
        }
        if (StringUtils.isBlank(request.getYearAndMonth())) {
            return CommonResult.fail("关键参数为空！");
        }
        try {
            AABillResult result = electricityConsumptionService.AABill(request.getYearAndMonth());
            return CommonResult.ok(result);
        } catch (Exception e) {
            log.info("ElectricityConsumptionController.AABill:", e);
            return CommonResult.fail(e.getMessage());
        }
    }
}
